package com.nexgo.payment.ui;

import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.nexgo.payment.R;
import com.nexgo.payment.adapter.ListMenuAdapter;
import com.nexgo.payment.trans.setting.SystemSettingsActivity;
import com.nexgo.payment.util.BarUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.TitleBuilder;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public abstract class ListMenuActivity extends BaseActivity {

    @BindView(R.id.list_menu)
    RecyclerView mListMenu;

    private ListMenuAdapter mAdapter;
    public static final int[] ATRRS = new int[]{
            android.R.attr.listDivider
    };
    private Drawable mDivider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setTransparentStatusBar21(this);
        setContentView(R.layout.list_menu);
        ButterKnife.bind(this);

        initTitle();
        initView();
    }

    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setTitleText(getActivityTitle())
                .build();
    }

    @OnClick(R.id.title_iv_left)
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()){
            case R.id.title_iv_left:
                finish();
                break;
            default:
                break;
        }
    }

    private void initView() {
        final TypedArray ta = obtainStyledAttributes(ATRRS);
        mDivider = ta.getDrawable(0);
        ta.recycle();
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        mListMenu.setLayoutManager(llm);
        mListMenu.setHasFixedSize(true);
        mAdapter = new ListMenuAdapter();
        mAdapter.setOnItemClickListener(mOnItemClickListener);
        mListMenu.setAdapter(mAdapter);
        mListMenu.addItemDecoration(new RecyclerView.ItemDecoration() {

            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                int left = parent.getPaddingLeft();
                int right = parent.getWidth() - parent.getPaddingRight();
                final int childCount = parent.getChildCount();
                for (int i = 0; i < childCount; i++) {
                    final View child = parent.getChildAt(i);

                    //获得child的布局信息
                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                    final int top = child.getBottom() + params.bottomMargin;
                    final int bottom = top + mDivider.getIntrinsicHeight();
                    mDivider.setBounds(left, top, right, bottom);
                    mDivider.draw(c);
                }
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
            }
        });
    }

    protected abstract String getActivityTitle();

    protected abstract void onItemClick(int position);

    protected void addItem(int stringId, int drawableId) {
        mAdapter.addItem(stringId, drawableId);
    }

    protected void addItem(int stringId) {
        mAdapter.addItem(stringId, 0);
    }

    ListMenuAdapter.OnItemClickListener mOnItemClickListener = position -> onItemClick(position);

    @Override
    public void finish() {
        super.finish();
        //应用参数设置界面不添加退出动画
        if (!(this instanceof SystemSettingsActivity)){
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        }
    }
}
